<template>
	<a-modal
		:title="title"
		:bodyStyle="{ maxHeight: '450px', overflow: 'auto' }"
		:visible="visible"
		:confirmLoading="confirmLoading"
		@ok="handleOk"
		@cancel="handleCancel"
		:destroyOnClose="true"
		:maskClosable="false"
	>
		<a-spin :spinning="confirmLoading">
			<a-form :form="form" autocomplete="off">
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="机构">
					<a-select v-decorator="['organization', { rules: [{ required: true, message: '所属机构' }] }]">
						<a-select-option :value="item.code" v-for="(item, index) in orglist" :key="index">{{ item.name }}</a-select-option>
					</a-select>
				</a-form-item>
				<div class="tip" style="color: #f00; text-align: center;">*导出该机构下除学生外所有账户</div>
			</a-form>
		</a-spin>
	</a-modal>
</template>

<script>
import { fileUrl } from '@/utils/base.js';
import { getOrglist, postAction } from '@/api';
import pick from 'lodash.pick';

export default {
	name: 'exportModal',
	data() {
		return {
			labelCol: {
				xs: {
					span: 24
				},
				sm: {
					span: 5
				}
			},
			wrapperCol: {
				xs: {
					span: 24
				},
				sm: {
					span: 16
				}
			},
			visible: false,
			confirmLoading: false,
			mdl: {},
			form: this.$form.createForm(this),
			title: '导出账号',
			orglist: [],
			isUserAdminRole: false //是否是超级管理员
		};
	},
	created() {
		/* 判断用户是否是管理员 */
		try {
			if (JSON.parse(localStorage.getItem('userInfo')).roleSign == 'admin') {
				this.isUserAdminRole = true;
			}
		} catch (e) {
			//TODO handle the exception
		}
	},
	methods: {
		initInfo() {
			if (this.isUserAdminRole) {
				this.getOrglist(); //查询机构
			}
			this.visible = true;
		},
		close() {
			this.$emit('close');
			this.visible = false;
		},
		handleOk() {
			const _this = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
				// 验证表单没错误
				if (!err) {
					this.exportExcel();
				}
			});
		},

		handleCancel() {
			this.close();
		},
		/* 查询机构 */
		getOrglist() {
			getOrglist().then(da => {
				this.orglist = da;
				/* 根据机构查询角色 */
				let code = '';
				if (this.orglist.length > 0) {
					this.form.setFieldsValue({
						organization: this.orglist[0].code
					});
					code = this.orglist[0].code;
				} else if (!this.isAdd) {
					code = this.mdl.organization;
				}
			});
		},
		exportExcel() {
			postAction(this.$api.sysManageapi.exportExcel, {
				organization: this.form.getFieldValue('organization')
			}).then(res => {
				if (res.code == 0) {
					let link = document.createElement('a');
					let filename = res.data.split('/');
					link.download = filename[filename.length - 1];
					link.style.display = 'none';
					link.href = fileUrl + '/plat' + res.msg;
					document.body.appendChild(link);
					link.click();
					document.body.removeChild(link);
					this.close();
				}
			});
		}
	}
};
</script>

<style scoped></style>
